<?php

namespace Mentees\Controller;

use Common\Controller\BaseController;



class MainController extends BaseController {
	public $userID;
    public $userName;
	public $userType;
    public $baseInfo=false;
    public function _initialize(){
    	parent::_initialize();
    	$this->userStatus = $this->checkUserStatus();
        if (!$this->userStatus) {
            $this->redirect(U("Home/Login/index"));
        }



        $this->userID = \Think\Crypt\Driver\Xxtea::decrypt(cookie('userID'),C('cookie_fix'));

        $this->userName = \Think\Crypt\Driver\Xxtea::decrypt(cookie('userName'),C('cookie_fix'));

        $this->userType = \Think\Crypt\Driver\Xxtea::decrypt(cookie('userType'),C('cookie_fix'));



        /*是否为准会员 仅对mentee起作用*/

        $this->userBasic = \Think\Crypt\Driver\Xxtea::decrypt(cookie('userBasic'),C('cookie_fix'));



        $this->assign('userID',$this->userID);

        $this->assign('userName',$this->userName);

        $this->assign('userType',$this->userType);

        $this->assign('userBasic',$this->userBasic);



        $user = M('Mentees')->where('userid='.$this->userID)->find();

        if ($user) {

            $this->baseInfo = true;

            $user['email']=$this->userName;

            $this->assign('user',$user);

        }



        // $map['isread']=0;

        // $map['toID']=$this->userID;

        // $msgNum = M('Message')->where($map)->count();

        // $this->assign('msgNum',$msgNum);	



        M("Mentees")->where("userid=".$this->userID."")->setField("last_action_time",time());



    }



    public function checkUserStatus(){

    	$userID = cookie('userID');

    	$userName = cookie('userName');

    	$userType = \Think\Crypt\Driver\Xxtea::decrypt(cookie('userType'),C('cookie_fix'));

        if(empty ($userName) || empty ($userID) || $userType!=1){ 

    		return false;

        }else{

        	return true;

        }

    }



    //发送站内信

    public function sendmsg(){

        $title=I("title");

        $content=I("content");

        $data['toID']=I("toID");

        $data['toName']=I("toName");

        $data['toFace']=I("toFace");

        $formID=$this->userID;

        if(empty($formID) || !isset($formID)){

            echo $this->echo_json_str(L('_no_login'),U("Home/Login/index"));exit;

        }

        if(empty($title) || !isset($title)){

            echo $this->echo_json_str(L('_title')." ".L('_noempty'));exit;

        }else{

            $data['title']=$title;

        }

        if(empty($content) || !isset($content)){

            echo $this->echo_json_str(L('_content')." ".L('_noempty'));exit;

        }else{

            $data['content']=$content;

        }

        $formUser=M("Mentees")->where("userid=".$formID."")->find();

        if(!$formUser){

           echo $this->echo_json_str(L('_not_info'),U("Profile/index"));exit; 

        }

        $data['fromID']=$formID;

        $data['fromName']=$formUser['nickname'];

        $data['fromFace']=$formUser['face'];

        $data['type']=1;

        $data['isread']=0;

        $data['createTime']=time();

        $list = M('Message')->add($data);

        if ($list) {
            unset($map);
            $map['userid']=$data['toID'];
            $toEmail=M("Member")->where($map)->getField("username");
            if($toEmail){
                $url = 'http://api.kf01.com/email/mentoring.php';
                $emailDate = array(
                        'email' => $toEmail,
                        'username' => $data['toName'],
                        'title' => $data['title'],
                        'content' => $data['content'],
                    );
                $this->https_post($url,$emailDate);
            }

            echo $this->echo_json_str("SUCCESS",L('_action_success'));exit;

        }else{

            echo $this->echo_json_str(L('_action_fail'));exit;

        }

    }



    //发送系统消息

    public function sendSysMsg($toID,$title,$content){

        $map['userid']=$toID;

        $userType=M("Member")->where($map)->getField("userType");

        if($userType){

            unset($map);

             $map['userid']=$toID;

            if($userType==1){

                $toUser=M("Mentees")->where($map)->find();

            }else{

                $toUser=M("Mentors")->where($map)->find();

            }

            $data['toID']=$toUser["userid"];

            $data['toName']=$toUser["nickname"];

            $data['toFace']=$toUser["face"];

            $data['fromID']=0;

            $data['fromName']='System';

            $data['fromFace']='';

            $data['type']=0;

            $data['isread']=0;

            $data['title']=$title;

            $data['content']=$content;

            $data['createTime']=time();

            M('Message')->add($data);

        }

    }

}